﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=Config company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;

namespace KM.DAT
{
    /// <summary>
    /// This is datConfig class
    /// </summary>

    public class datConfig
    {
        public datConfig()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa Config===
        public void createEdit(entConfig entC)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_ConfigCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[20];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[1] = new SqlParameter("Mail_Smtp", DbType.Int32);
            sqla[2] = new SqlParameter("Mail_Port", DbType.Int32);
            sqla[3] = new SqlParameter("Mail_Info", DbType.Int32);
            sqla[4] = new SqlParameter("Mail_Noreply", DbType.Int32);
            sqla[5] = new SqlParameter("Mail_Password", DbType.Int32);
            sqla[6] = new SqlParameter("PlaceHead", DbType.Int32);
            sqla[7] = new SqlParameter("PlaceBody", DbType.Int32);
            sqla[8] = new SqlParameter("GoogleId", DbType.Int32);
            sqla[9] = new SqlParameter("Contact", DbType.Int32);
            sqla[10] = new SqlParameter("DeliveryTerms", DbType.Int32);
            sqla[11] = new SqlParameter("PaymentTerms", DbType.Int32);
            sqla[12] = new SqlParameter("FreeShipping", DbType.Int32);
            sqla[13] = new SqlParameter("Copyright", DbType.Int32);
            sqla[14] = new SqlParameter("Title", DbType.Int32);
            sqla[15] = new SqlParameter("Description", DbType.Int32);
            sqla[16] = new SqlParameter("Keyword", DbType.Int32);
            sqla[17] = new SqlParameter("Lang", DbType.Int32);
            sqla[18] = new SqlParameter("Helpsize", DbType.Int32);
            sqla[19] = new SqlParameter("Location", DbType.Int32);

            sqla[0].Value = entC.Id;
            sqla[1].Value = entC.Mail_Smtp;
            sqla[2].Value = entC.Mail_Port;
            sqla[3].Value = entC.Mail_Info;
            sqla[4].Value = entC.Mail_Noreply;
            sqla[5].Value = entC.Mail_Password;
            sqla[6].Value = entC.PlaceHead;
            sqla[7].Value = entC.PlaceBody;
            sqla[8].Value = entC.GoogleId;
            sqla[9].Value = entC.Contact;
            sqla[10].Value = entC.DeliveryTerms;
            sqla[11].Value = entC.PaymentTerms;
            sqla[12].Value = entC.FreeShipping;
            sqla[13].Value = entC.Copyright;
            sqla[14].Value = entC.Title;
            sqla[15].Value = entC.Description;
            sqla[16].Value = entC.Keyword;
            sqla[17].Value = entC.Lang;
            sqla[18].Value = entC.Helpsize;
            sqla[19].Value = entC.Location;


            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall Config===
        public List<entConfig> getAll()
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_ConfigGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;

            dr = cmd.ExecuteReader();
            entConfig entC = new entConfig();
            List<entConfig> lst = new List<entConfig>();
            while (dr.Read())
            {
                entC = new entConfig();
                entC.Id = Convert.ToInt32(dr["Id"].ToString());
                entC.Mail_Smtp = dr["Mail_Smtp"].ToString();
                entC.Mail_Port = Convert.ToInt16(dr["Mail_Port"].ToString());
                entC.Mail_Info = dr["Mail_Info"].ToString();
                entC.Mail_Noreply = dr["Mail_Noreply"].ToString();
                entC.Mail_Password = dr["Mail_Password"].ToString();
                entC.PlaceHead = dr["PlaceHead"].ToString();
                entC.PlaceBody = dr["PlaceBody"].ToString();
                entC.GoogleId = dr["GoogleId"].ToString();
                entC.Contact = dr["Contact"].ToString();
                entC.DeliveryTerms = dr["DeliveryTerms"].ToString();
                entC.PaymentTerms = dr["PaymentTerms"].ToString();
                entC.FreeShipping = Convert.ToDouble(dr["FreeShipping"].ToString());
                entC.Copyright = dr["Copyright"].ToString();
                entC.Title = dr["Title"].ToString();
                entC.Description = dr["Description"].ToString();
                entC.Keyword = dr["Keyword"].ToString();
                entC.Lang = dr["Lang"].ToString();
                entC.Helpsize = dr["Helpsize"].ToString();
                entC.Location = Convert.ToInt32(dr["Location"].ToString());

                lst.Add(entC);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo Config===
        public entConfig getInfo(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_ConfigGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = Id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entConfig entC = new entConfig();
            if (dr.Read())
            {
                entC.Id = Convert.ToInt32(dr["Id"].ToString());
                //entC.Mail_Smtp = dr["Mail_Smtp"].ToString();
                //entC.Mail_Port = Convert.ToInt16(dr["Mail_Port"].ToString());
                //entC.Mail_Info = dr["Mail_Info"].ToString();
                //entC.Mail_Noreply = dr["Mail_Noreply"].ToString();
                //entC.Mail_Password = dr["Mail_Password"].ToString();
                //entC.PlaceHead = dr["PlaceHead"].ToString();
                //entC.PlaceBody = dr["PlaceBody"].ToString();
                //entC.GoogleId = dr["GoogleId"].ToString();
                //entC.Contact = dr["Contact"].ToString();
                //entC.DeliveryTerms = dr["DeliveryTerms"].ToString();
                //entC.PaymentTerms = dr["PaymentTerms"].ToString();
                //entC.FreeShipping = Convert.ToDouble(dr["FreeShipping"].ToString());
                //entC.Copyright = dr["Copyright"].ToString();
                entC.Title = dr["Title"].ToString();
                entC.Description = dr["Description"].ToString();
                entC.Keyword = dr["Keyword"].ToString();
                //entC.Lang = dr["Lang"].ToString();
                //entC.Helpsize = dr["Helpsize"].ToString();
                //entC.Location = Convert.ToInt32(dr["Location"].ToString());

            }
            dr.Close();
            sqlConnection.Close();
            return entC;
        }
        #endregion

        #region === xoa Config===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_ConfigDeleteById", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}
